package com.eostoken.eos_wallet.helper;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.R;
import com.qmuiteam.qmui.layout.QMUIFrameLayout;
import com.qmuiteam.qmui.skin.QMUISkinHelper;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.popup.QMUIFullScreenPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopups;

import java.util.List;

public class DialogHelper {

    private static int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    public static void showRationaleDialog(Context context, PermissionUtils.OnRationaleListener.ShouldRequest shouldRequest) {
        new QMUIDialog.MessageDialogBuilder(context)
                .setTitle(StringUtils.getString(android.R.string.dialog_alert_title))
                .setMessage(StringUtils.getString(R.string.permission_rationale_message))
                .setSkinManager(QMUISkinManager.defaultInstance(context))
                .addAction(StringUtils.getString(android.R.string.cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        shouldRequest.again(false);
                    }
                })
                .addAction(0, StringUtils.getString(android.R.string.ok), QMUIDialogAction.ACTION_PROP_POSITIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        shouldRequest.again(true);
                    }
                })
                .create().show();
    }

    public static QMUIDialog showEditTextDialog(Context context, String title, String hint, String ok, String cancel, DialogConfirmListener listener, boolean isPassword) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(context);
        builder.setTitle(title)
                .setSkinManager(QMUISkinManager.defaultInstance(context))
                .setPlaceholder(hint)
                .setInputType(isPassword ? InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT)
                .addAction(cancel, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        builder.getEditText().setText("");
                        dialog.dismiss();
                    }
                })
                .addAction(ok, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        listener.onClick(dialog, builder.getEditText(), index);
                    }
                });
        return builder.create(mCurrentDialogStyle);
    }

    public static QMUIDialog showLongMessageDialog(Context context, String title, String message, String textbtn1, String textbtn2, QMUIDialogAction.ActionListener listener1, QMUIDialogAction.ActionListener listener2) {
        return new QMUIDialog.MessageDialogBuilder(context)
                .setTitle(title)
                .setSkinManager(QMUISkinManager.defaultInstance(context))
                .setMessage(message)
                .addAction(textbtn1, listener1)
                .addAction(textbtn2, listener2)
                .setCancelable(false)
                .create(mCurrentDialogStyle);
    }


    public static QMUIFullScreenPopup showImage(Context context, Bitmap bitmap, int size) {
        QMUISkinValueBuilder builder = QMUISkinValueBuilder.acquire();
        QMUIFrameLayout frameLayout = new QMUIFrameLayout(context);
        frameLayout.setBackground(QMUIResHelper.getAttrDrawable(context, R.attr.qmui_skin_support_popup_bg));
        builder.background(R.attr.qmui_skin_support_popup_bg);
        QMUISkinHelper.setSkinValue(frameLayout, builder);
        frameLayout.setRadius(QMUIDisplayHelper.dp2px(context, 5));
        int padding = QMUIDisplayHelper.dp2px(context, 20);
        frameLayout.setPadding(padding, padding, padding, padding);

        AppCompatImageView imageView = new AppCompatImageView(context);
        imageView.setImageBitmap(bitmap);
        builder.clear();
        QMUISkinHelper.setSkinValue(imageView, builder);
        builder.release();
        size = QMUIDisplayHelper.dp2px(context, size);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(size, size);
        frameLayout.addView(imageView, lp);

        QMUIFullScreenPopup popup = QMUIPopups.fullScreenPopup(context)
                .addView(frameLayout)
                .closeBtn(true)
                .skinManager(QMUISkinManager.defaultInstance(context))
                .onBlankClick(new QMUIFullScreenPopup.OnBlankClickListener() {
                    @Override
                    public void onBlankClick(QMUIFullScreenPopup popup) {
                        //点击到空白区域
                    }
                })
                .onDismiss(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                    }
                });
        return popup;
    }

    // showSimpleBottomSheetList(false, true, false, "This is Title!!!", 100, true, true);
    // ================================ 生成不同类型的BottomSheet
    public static QMUIBottomSheet showSimpleBottomSheetList(Context context, boolean gravityCenter, boolean addCancelBtn, boolean withIcon,
                                                            String title, List<Integer> iconIdList, List<String> itemList,
                                                            boolean allowDragDismiss, boolean withMark,
                                                            QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener listener) {
        QMUIBottomSheet.BottomListSheetBuilder builder = new QMUIBottomSheet.BottomListSheetBuilder(context);
        builder.setGravityCenter(gravityCenter)
                .setSkinManager(QMUISkinManager.defaultInstance(context))
                .setTitle(title)
                .setAddCancelBtn(addCancelBtn)
                .setAllowDrag(allowDragDismiss)
                .setNeedRightMark(withMark)
                .setOnSheetItemClickListener(listener);
//        new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
//                        @Override
//                    public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
//                        dialog.dismiss();
////                        Toast.makeText(context, "Item " + (position + 1), Toast.LENGTH_SHORT).show();
//                    }
//                });
        for (int i = 0; i < itemList.size(); i++) {
            if (withIcon) {
                builder.addItem(ContextCompat.getDrawable(context, iconIdList.get(i)), itemList.get(i));
            } else {
                builder.addItem(itemList.get(i));
            }

        }
        return builder.build();
    }

    /**
     * 显示更新对话框
     *
     * @param remark 要更新的内容说明
     */
    public static Dialog showUpdateDialog(Context context, String remark, View.OnClickListener listener) {
        View view = View.inflate(context, R.layout.dialog_updata, null);
        final Dialog updateDialog = new Dialog(context, R.style.loading_dialog);
        updateDialog.setContentView(view);
        updateDialog.setCancelable(false);
        updateDialog.setCanceledOnTouchOutside(false);
        updateDialog.show();

        RelativeLayout detailsLayout = (RelativeLayout) view.findViewById(R.id.detailsLayout);
        TextView udpata_view_text = (TextView) view.findViewById(R.id.updateInstructionsTv);
        Button dialog_update_download_bt = (Button) view.findViewById(R.id.completeBtn);
        ImageView updateIv = (ImageView) view.findViewById(R.id.updateIv);

        udpata_view_text.setText(remark);
        int height = SizeUtils.getMeasuredHeight(updateIv);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) detailsLayout.getLayoutParams();
        params.topMargin = height / 2;
        detailsLayout.setLayoutParams(params);
        detailsLayout.requestLayout();

        dialog_update_download_bt.setOnClickListener(listener);
//        new View.OnClickListener() {
//            @Override
//            public void onClick(View arg0) {
//                updateDialog.dismiss();
//                downloadApk(url);
//            }
//        });
        return updateDialog;
    }
}
